<?php
namespace Admin\Model;
use Think\Model;
class ArticleModel extends Model {
	protected $tableName = 'article'; 
	protected $_validate = array(
		array('title','require','标题不能为空！'), 
		array('content','require','内容不能为空！'), 
	);

	/**
	 * 获取新闻
	 */
	public function mselect() {
		if(!empty($_POST['id'])) {
			$where['cid'] = I('post.id','','intval');
		}
		$order = C('ARTICLEORDER');

		$count      = $this->where($where)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		//进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $this->where($where)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();

		return array(
			'list' => $list,
			'page' => $show,
		);
	}

	/**
	 * 添加修改
	 */
	public function msave() {
		$data['title'] = I('post.title','','strip_tags');
		$data['content'] = I('post.content','');
		$data['sort'] = I('post.sort','','intval');
		$data['cid'] = I('post.cid','','intval');
		if(!empty($_FILES['img']['name'])) {
		$data['img'] = I('post.img','','strip_tags');
		}
		$where['id'] = I('post.id','','intval');
		if(!empty($where['id'])) {
			$msg = $this->where($where)->save($data);
		} else {
			$data['time'] = time();
			$msg = $this->add($data);
		}
		return $msg;
	}

}
